/***************************************************************************
 *   Copyright (C) 2009 by maarten,,,   *
 *   maarten@maarten-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "objects/objectlist.h"
#include "objects/object.h"
#include "objects/vertex.h"
#include "objects/objecttexture.h"
#include "objects/normal.h"
#include "objects/plane.h"

#include <QFile>
#include <QTextStream>
#include <QString>
#include <QIODevice>
#include <QStringList>
#include <iostream>
using namespace std;

namespace object {

ObjectList::ObjectList()
{
}

ObjectList::~ObjectList()
{
    for(int i = 0; i < m_objectList.size(); ++i)
        delete m_objectList[i];
}

void ObjectList::clean()
{
    for(int i = 0; i < m_objectList.size(); ++i)
      delete m_objectList[i];
    
    m_objectList.clear();
}

bool ObjectList::loadObject(QString objFile)
{
    QFile newFile( objFile );

    if (!newFile.open(QIODevice::ReadOnly)) {
        qDebug("No such file...:");
        return false; 
    }

    Object *newObject = new Object();

    QTextStream stream( &newFile ); 
    QString line = stream.readLine();

    while(!line.isNull()){
        parseLine(line, newObject);
        line = stream.readLine();
    }

    m_objectList.push_back(newObject);

    return true;
}

void ObjectList::parseLine(QString line, Object *targetObj)
{
    QStringList command = line.split(" ");

    if(command.at(0).compare("v") == 0){ // vertex toevoegen
        //cout << "READING VERTEX\n";
        targetObj->addVertex(new Vertex(command[1].toFloat(),
command[2].toFloat(), command[3].toFloat() ));
    } else if(command.at(0).compare("vn") == 0){ // normal toevoegen
        //cout << "READING NORMAL\n";
        targetObj->addNormal(new Normal(command[1].toFloat(),
command[2].toFloat(), command[3].toFloat() ));
    } else if(command.at(0).compare("vt") == 0){ // texture toevoegen
        //cout << "READING TEXTURE\n";
        targetObj->addTexture(new ObjectTexture(command[1].toFloat(),
command[2].toFloat() ));
    } else if(command.at(0).compare("f") == 0){ // plane toevoegen
        //cout << "READING PLANE\n";
        Plane *tempPlane = new Plane();
        for(int i = 1; i < command.size(); ++i){
            QStringList cornerString = command[i].split("/");
              switch(cornerString.size()){ // texture and normal given?
                case 1:
                  tempPlane->addCorner(
                    targetObj->getVertex(cornerString[0].toInt()-1));
                  break;
                case 2:
                  tempPlane->addCorner(
                    targetObj->getVertex(cornerString[0].toInt()-1),
                    targetObj->getTexture(cornerString[1].toInt()-1));
                  break;
                case 3:
                  //cout << "TES ER ZO ENE";
                  tempPlane->addCorner(
                    targetObj->getVertex(cornerString[0].toInt()-1),
                    targetObj->getTexture(cornerString[1].toInt()-1),
                    targetObj->getNormal(cornerString[2].toInt()-1));
                  break;
                default:
                  cout << "ERROR: wrong format of plane\n";
                  
              }
            }
            targetObj->addPlane(tempPlane);        
    }else{
      ;//cout << "Not recognized...\n";
    }
}

void ObjectList::printObjects()
{
    for(int i = 0; i < m_objectList.size(); ++i)
        m_objectList[i]->print();
}

}